<template>
    <div style="width: 100%;">
        <div class="header" style="width: 100%;background-color: white">
            <el-page-header @back="goBack" content="分包招标采购计划">
            </el-page-header>
            <el-divider></el-divider>
        </div>

        <div style="width: 90%;margin: 0 auto">
            <div style="width: 100%;">
                <span style="font-weight: 700;font-size: 20px">|基本信息</span>
            </div>
            <el-divider></el-divider>
            <el-form style="width: 100%;" label-width="100px">
                <el-container>
                    <el-form-item label="计划编号：">
                        <el-input v-model="subTenderPlan.id" readonly style="width: 500px;"></el-input>
                    </el-form-item>
                    <el-form-item label="日期：" style="margin-left: 10px">
                        <el-input v-model="subTenderPlan.date" readonly style="width: 500px;"></el-input>
                    </el-form-item>
                </el-container>
                <el-container>
                    <el-form-item label="项目选择：">
                        <el-input v-model="subTenderPlan.projectName" readonly style="width: 500px;"
                                  @focus="projectDialogVisible=true"></el-input>
                        <el-input v-model="subTenderPlan.proId" v-show="false"></el-input>
                    </el-form-item>
                    <el-form-item label="编制人：" style="margin-left: 10px">
                        <el-input v-model="subTenderPlan.planEditor" readonly style="width: 500px;"></el-input>
                    </el-form-item>
                </el-container>
                <el-container>
                    <el-form-item label="备注：">
                        <el-input v-model="subTenderPlan.comment" type="textarea"
                                  style="width: 1110px;"></el-input>
                    </el-form-item>
                </el-container>
            </el-form>
        </div>

        <multi-file-upload
                style="width: 90%;margin: 20px auto"
                title="附件"
                action="http://localhost/uploads"
                :params="{folder: 'subTenderPlan'}"
                :headers="{token: token}"
                :show-button="!checkRoute"
                @uploadSuccess="handleUploadSuccess"
                :parent-file-list="fileList"
        ></multi-file-upload>

        <div style="width: 90%;margin: 30px auto">
            <div style="width: 100%;">
                <div style="font-weight: 700;font-size: 20px;display: flex">
                    <div>|分包招标采购明细</div>
                    <div style="margin-left: 30px">
                        <el-button v-if="!checkRoute" size="mini" type="primary" @click="handleAddRow">增行</el-button>
                    </div>
                </div>
            </div>
            <el-divider></el-divider>
            <el-table :data="subTenderDetails">
                <el-table-column prop="index" v-if="false"></el-table-column>
                <el-table-column label="分包项目" prop="subProject">
                    <template slot-scope="scope">
                        <el-input v-model="scope.row.subProject"></el-input>
                    </template>
                </el-table-column>
                <el-table-column label="分包工作内容" prop="subContent">
                    <template slot-scope="scope">
                        <el-input v-model="scope.row.subContent"></el-input>
                    </template>
                </el-table-column>
                <el-table-column label="计划招标时间" prop="tenderDate">
                    <template slot-scope="scope">
                        <el-date-picker
                                v-model="scope.row.tenderDate"
                                type="date"
                                value-format="yyyy-MM-dd"
                                :pickerOptions="pickerOptions"
                                :clearable="true"
                                style="width: 100%;"
                                placeholder="选择日期">
                        </el-date-picker>
                    </template>
                </el-table-column>
                <el-table-column label="组织招标方" prop="tenderer">
                    <template slot-scope="scope">
                        <el-input v-model="scope.row.tenderer" readonly></el-input>
                    </template>
                </el-table-column>
                <el-table-column label="分包方式" prop="subTenderType">
                    <template slot-scope="scope">
                        <el-select v-model="scope.row.subTenderType" :value="scope.row.subTenderType">
                            <el-option label="包工包料" value="1"></el-option>
                            <el-option label="劳务" value="2"></el-option>
                            <el-option label="包工及部分材料" value="3"></el-option>
                            <el-option label="其他" value="4"></el-option>
                        </el-select>
                    </template>
                </el-table-column>
                <el-table-column label="分包商选择方式" prop="subProviderChoose">
                    <template slot-scope="scope">
                        <el-select v-model="scope.row.subProviderChoose" :value="scope.row.subProviderChoose">
                            <el-option label="公司选定" value="1"></el-option>
                            <el-option label="业主选定" value="2"></el-option>
                            <el-option label="项目选定" value="3"></el-option>
                            <el-option label="项目选择公司批准" value="4"></el-option>
                            <el-option label="业主指定我方签合同" value="5"></el-option>
                            <el-option label="业主项目共同选择" value="6"></el-option>
                            <el-option label="其他" value="7"></el-option>
                        </el-select>
                    </template>
                </el-table-column>
                <el-table-column label="候选分包商" prop="subProviders">
                    <template slot-scope="scope">
                        <el-input v-model="scope.row.subProviders"></el-input>
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="100px">
                    <template slot-scope="scope">
                        <el-button v-if="!checkRoute" type="danger" @click="handleRemoveRow(scope.row)">删行</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <el-divider></el-divider>
        <div style="width: 100%;margin-left: 90px">
            <el-button type="primary" @click="handleSubmit" v-if="!checkRoute">确认提交</el-button>
        </div>

        <el-dialog
                title="项目选择"
                :visible.sync="projectDialogVisible"
                width="70%">
            <project-info-list @row="handleSetProject" v-if="projectDialogVisible"></project-info-list>
        </el-dialog>
    </div>
</template>

<script>
    import {DateUtil} from "../../../model/common/DateUtil";
    import {SubTenderPlanService} from "../../../model/gs/SubTenderPlanService";
    import {SubTenderDetailService} from "../../../model/gs/SubTenderDetailService";
    import {AttachemenInfoService} from "../../../model/gs/AttachemenInfoService";

    const ProjectInfoList = () => import('../../common/projectInfo/ProjectInfoList.vue')

    const subTenderPlanService = SubTenderPlanService.getInstance()
    const subTenderDetailService = SubTenderDetailService.getInstance()
    const attachemenInfoService = AttachemenInfoService.getInstance()

    export default {
        data() {
            return {
                subTenderPlan: {
                    id: '',
                    date: DateUtil.today(),
                    planEditor: sessionStorage.getItem('realName')
                },
                projectDialogVisible: false,
                fileList: [],
                subTenderDetails: [],
                //时间校验配置
                pickerOptions: {
                    disabledDate(time) {
                        return time.getTime() < Date.now();//设置选择今天之后的日期（不能选择当天时间）
                    }
                }
            }
        },
        methods: {
            getPlanById(id) {
                subTenderPlanService.getById(id).then(response => {
                    if (response.data.code === 0) {
                        this.subTenderPlan = response.data.data
                    }
                })
            },
            getDetailList(id) {
                subTenderDetailService.getByPlan(id).then(response => {
                    if (response.data.code === 0) {
                        this.subTenderDetails = response.data.data
                    }
                })
            },
            getFileList(id) {
                attachemenInfoService.getById(id).then(response => {
                    if (response.data.code === 0) {
                        response.data.data.forEach(item => {
                            this.fileList.push({
                                fileName: item.fileName,
                                uploadPerson: this.subTenderPlan.planEditor,
                                uploadDate: item.uploadDate,
                                filePath: item.filePath
                            })
                        })
                    }
                })
            },
            getMaxCode() {
                subTenderPlanService.getMaxCode().then(response => {
                    if (response.data.code === 0) {
                        this.subTenderPlan.id = response.data.data
                    }
                })
            },
            handleSetProject(row) {
                this.subTenderPlan.proId = row.id
                this.subTenderPlan.projectName = row.projectName
                this.projectDialogVisible = false
            },
            handleUploadSuccess(ids){
                this.subTenderPlan.attachemenIds = ids
            },
            handleAddRow() {
                this.subTenderDetails.push({
                    subtenderId: this.subTenderPlan.id,
                    index: this.subTenderDetails.length,
                    tenderDate: '',
                    subProject: '',
                    subContent: '',
                    tenderer: '公开招标',
                    subTenderType: '1',
                    subProviderChoose:'1',
                    subProviders:''
                })
            },
            handleSubmit() {
                subTenderPlanService.save(this.subTenderPlan, this.subTenderDetails).then(response => {
                    if (response.data.code === 0) {
                        this.$message.success({
                            message: '申请成功',
                            duration: 1000
                        })
                        this.$router.push({
                            path: '/SubPlanList'
                        })
                    }
                })
            },
            handleRemoveRow(row) {
                this.subTenderDetails.splice(row.index, 1)
            },
            goBack() {
                history.back()
            }
        },
        computed: {
            token: {
                get() {
                    return this.$store.state.token
                }
            },
            checkRoute(){
                return Object.keys(this.$route.params).length != 0
            }
        },
        created() {
            if (Object.keys(this.$route.params).length == 0) {
                this.getMaxCode()
            } else {
                const row = this.$route.params.row
                this.getPlanById(row.id)
                this.getDetailList(row.id)
                this.getFileList(row.attachemenIds)
            }
        },
        components: {
            ProjectInfoList
        }
    }
</script>

<style scoped>

</style>
